﻿

Namespace Processing
    Public Class DeleteVisualStudioBackupFiles
        Inherits BaseProcess(Of DeleteWebSiteCache)
        Implements Interfaces.IProcess


        Public Sub Action() Implements Interfaces.IProcess.Action
            CleanBackupFiles()
        End Sub

        Public ReadOnly Property Name As String Implements Interfaces.IProcess.Name
            Get
                Return "Cleaning Project Backup Files"
            End Get
        End Property

        Private Shared Sub CleanBackupFiles()
            DeleteFiles(&H7D5)
            DeleteFiles(&H7D8)
            DeleteFiles(&H7DA)
        End Sub

        Private Shared Sub DeleteDirectoryPass(ByVal rootDir As String)
            If My.Computer.FileSystem.DirectoryExists(rootDir) Then
                My.Computer.FileSystem.GetDirectories(rootDir).AsParallel.ForAll(Sub(deldir)
                                                                                     Try
                                                                                         My.Computer.FileSystem.DeleteDirectory(deldir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                                                                                     Catch ex As Exception
                                                                                         ' Do nothing just skip
                                                                                     End Try
                                                                                 End Sub)
            End If
        End Sub

        Private Shared Sub DeleteFiles(ByVal version As Integer)
            Dim myDocuments As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
            Dim directory As String = String.Format("{0}\Visual Studio {1}\Backup Files", myDocuments, version)
            If My.Computer.FileSystem.DirectoryExists(directory) Then
                For x As Integer = 0 To 3
                    DeleteDirectoryPass(directory)
                Next
            End If
        End Sub

    End Class
End Namespace

